package com.bizmerce.ebms.engine;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Vector;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.soap.SOAPFaultException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.NodeList;

import com.bizmerce.ebms.Generator.MessageGenerator;
import com.bizmerce.ebms.vo.SoapHeaderVO;


public class InBoundProcessor {
	
	private Log logger = LogFactory.getLog(InBoundProcessor.class);
	
	public InBoundProcessor() {
		
	}
	
	public static void main(String args[]) {
		
		String fileName 	= "C:/Documents and Settings/Microsoft/바탕 화면/soap/test/R01_200909151533194826.pkg";
		
		//MessageGenerator mg 	  = new MessageGenerator();
		String soapFileName       = "";     // SOAP MESSAGE명
		String xmlFileName        = "";     // XML명
		//SoapHeaderVO soapHeaderVo = new SoapHeaderVO();   // SOAP Header Value Object
		
		try {
			
			//Vector fileVec = mg.doPkgParse(fileName);
			
			int p = fileName.lastIndexOf(".");
			
			soapFileName = fileName.substring(0, p) + ".soap";
			xmlFileName  = fileName.substring(0, p) + ".xml";
		
			MessageFactory mf       = MessageFactory.newInstance();
			//InputStream in          = new FileInputStream(soapFileName);
			InputStream in          = new FileInputStream(fileName);
			
			MimeHeaders mimeHeaders = new MimeHeaders();
			
			
			mimeHeaders.setHeader("Content-Type", "multipart/related; " +
								  "boundary=\"--=20090915153335453481--\"; " +
								  "type=\"text/xml\"; " +
								  "start=\"<--=20090915153335453481-->\" ");
			mimeHeaders.setHeader("soapaction", "ebXML");
			mimeHeaders.setHeader("Content-Language", "UTF-8");
			mimeHeaders.setHeader("cache-control", "no-cache");
			mimeHeaders.setHeader("accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
			
			System.out.println("1111111111");
			
			SOAPMessage soapMessage = mf.createMessage(mimeHeaders, in);
			
			System.out.println("22222222222");
			
			Iterator iter = soapMessage.getAttachments();
			
			System.out.println(iter.hasNext());

			
		    SOAPPart     soapPart     = soapMessage.getSOAPPart();
		    SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

		    SOAPHeader   soapHeader   = soapEnvelope.getHeader();
		    SOAPBody     soapBody     = soapEnvelope.getBody();
		     
		    NodeList nodeList = soapHeader.getElementsByTagName("eb:PartyId");
		     
		    if (nodeList.getLength() > 0) {
		    	 
		    	String partyId = nodeList.item(0).getFirstChild().getNodeValue();
		    	System.out.println("partyId : " + partyId);
		    	 
		    }
		     
		     
		    nodeList = soapHeader.getElementsByTagName("eb:Role");
		     
		    if (nodeList.getLength() > 0) {
		    	 
		    	String role  = nodeList.item(0).getFirstChild().getNodeValue();
		    	System.out.println("role : " + role);
		    	 
		    }
		     
		    /*
		    for (int i = 0 ; i < nodeList2.getLength() ; i++) {
		    	 
		    	Node n = nodeList2.item(i);
		    	 
		    	System.out.println(" NodeName  : " + n.getNodeName());
		    	System.out.println(" NodeValue : " + n.getNodeValue());
		    	 
		    }
		    */
		     
		    in.close();
		} catch (IOException e) {
			
			e.printStackTrace();

		//} catch (SOAPFaultException e) {
			
			//e.printStackTrace();
			
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		

		
		/*
		
		MessageGenerator mg = new MessageGenerator();
		
		String soapFileName       = "";     // SOAP MESSAGE명
		String xmlFileName        = "";     // XML명
		SoapHeaderVO soapHeaderVo = null;   // SOAP Header Value Object
		
		List attachFiles = new ArrayList(); // 첨부파일 

		
		try {
		
			Vector fileVec = mg.doPkgParse(fileName);
			
			for (int i = 0; i < fileVec.size(); i++) {
				
				String sfilepath = ((PayloadInfoVO) fileVec.get(i)).getSavedPath();
				
				if (i == 0) {
					soapFileName = sfilepath;
					
				} else if (i == 1) {
					xmlFileName = sfilepath;
					
				} else if (i > 1) {
					attachFiles.add(fileVec.get(i));
					
				}
				
			}
			
			boolean namespaces        	= true;
			boolean namespacePrefixes 	= true;
			boolean validation 			= false;
			boolean externalDTD 		= false;
			boolean schemaValidation 	= false;
			boolean schemaFullChecking 	= false;
			boolean dynamicValidation 	= false;
			XMLReader parser 			= null;
			
			parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
			parser.setFeature("http://xml.org/sax/features/namespaces", 						namespaces);
			parser.setFeature("http://xml.org/sax/features/namespace-prefixes", 				namespacePrefixes);
			parser.setFeature("http://xml.org/sax/features/validation", 						validation);
			parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", externalDTD);
			parser.setFeature("http://apache.org/xml/features/validation/schema", 				schemaValidation);
			parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", schemaFullChecking);
			parser.setFeature("http://apache.org/xml/features/validation/dynamic", 				dynamicValidation);
		
		 
			soapHeaderVo = new SoapHeaderVO();
			SoapHeaderHandler headerHandler = new SoapHeaderHandler(soapHeaderVo);
			
			parser.setErrorHandler(headerHandler);
			if (parser instanceof XMLReader)
				parser.setContentHandler(headerHandler);
			
			parser.parse(new InputSource(new FileInputStream(new File(soapFileName))));
			
			System.out.println("===========================");
			
			System.out.println(" ::: " + soapHeaderVo.getFromRole() + ":::");
			
			System.out.println("===========================");
			
		} catch (SAXNotRecognizedException e) {
				
			e.printStackTrace();

		} catch (SAXException e) {
				
			e.printStackTrace();

		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
		*/
		
	}

}
